package com.cuitrip.business.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.cuitrip.apiservice.SettingBusiness;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.logtrack.AppLog;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.MessageUtils;
import com.lab.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created on 7/16.
 */
public class SuggestActivity extends CustomUiFragmentActivity implements IProxyCallback {

    public static final int LIMIT = 1500;

    @Bind(R.id.edit_block)
    EditText editText;


    @Bind(R.id.edit_count)
    TextView textCount;
    ApiProxy apiProxy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.page_advice);
        apiProxy=new ApiProxy(this);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        textCount.setText(getString(R.string.txt_count_format, editText.getText().length(), LIMIT));
    }

    public void initData() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                try {
                    int nSelStart = 0;
                    int nSelEnd = 0;
                    boolean nOverMaxLength = false;
                    nSelStart = editText.getSelectionStart();
                    nSelEnd = editText.getSelectionEnd();
                    nOverMaxLength = (editable.length() > LIMIT) ? true : false;
                    textCount.setText(getString(R.string.txt_count_format, editable.length(),LIMIT));
                    if (nOverMaxLength) {
                        editable.delete(nSelStart - 1, nSelEnd);
                        editText.setTextKeepState(editable);
                        return;
                    }
                } catch (Exception e) {
                    AppLog.w(e.toString());
                }
            }
        });
    }

    @OnClick(R.id.submit_action)
    protected void submitAction() {
        if (editText.getText().length() == 0){
            Animation shakeAnimation = AnimationUtils.loadAnimation(this, R.anim.shake);
            editText.startAnimation(shakeAnimation);
            return;
        }
        showNoCancelDialog();
        SettingBusiness.submitAdvice(apiProxy,editText.getText().toString());
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=getString(R.string.setting_advice);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        hideNoCancelDialog();
        CtApiResponse response = (CtApiResponse) result.getData();
        if (response!= null && response.code == CtResponseCode.RESULT_OK){
                SubmitSuccessActivity.startActivity(SuggestActivity.this,getString(R.string.feedback_submit_suc),getString(R.string.setting_advice_submit_feedback));
                finish();
        }else{
            if (response != null && !TextUtils.isEmpty(response.msg)) {
                MessageUtils.showToast(response.msg);
            }
        }
        return false;
    }
}
